<?php
// +----------------------------------------------------------------------
// | 文件目录：D:\information\phone-bill-interface-main\app\Http\Controllers\Api
// +----------------------------------------------------------------------
// | 文件名：CardController.php
// +----------------------------------------------------------------------
// | 文件路径：D:\information\phone-bill-interface-main\app\Http\Controllers\Api\CardController.php
// +----------------------------------------------------------------------
// | 文件功能： 优惠券相关功能控制器
// +----------------------------------------------------------------------
// | 作者：李星星
// +----------------------------------------------------------------------
// | 创建时间：2025-05-15 13:29:11
// +----------------------------------------------------------------------
namespace App\Http\Controllers\Api;

use App\Enum\CardRecordCode;
use App\Models\CardModel;
use App\Models\PayRecordModel;
use App\Utils\Message;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class CardController extends BaseController
{
    public function query(
        Request $request,
        CardModel $cardModel,
        PayRecordModel $payRecordModel
    ) {
        $phone = $request->phone;
        if (empty($phone)) {
            return Message::show_message(false, '手机号码不能为空');
        }

        // 判断手机号码是否已经支付成功
        $payInfo = $payRecordModel->where(['phone' => $phone])->orderBy('record_id', 'desc')->first();
        if (empty($payInfo)) {
            return Message::show_message(false, '您暂无购买记录，领取失败');
        }

        // 判断是否已激活
        if ($cardModel->where(['phone' => $phone])->exists()) {
            return Message::show_message(false, '您已激活，可直接充值');
        }

        DB::beginTransaction();
        try {
            $cardModel->create([
                'status' => CardRecordCode::IS_USE_CODE,
                'phone' => $phone,
                'product_id' => env('TOPUP_PRODUCT_ID', ''),
                'card_uuid' => Str::uuid(),
                'remaining_num' => env('TOPUP_NUMBER'),
                'get_time' => now(),
                'due_time' => now()->addYears((int) env('CARD_VALIDITY', 1)),
            ]);
            DB::commit();
            return Message::show_message(true, '充值卡领取成功');
        } catch (Exception $error) {
            DB::rollBack();
            return Message::show_message(true, $error->getMessage());
        }
    }

    /**
     * 获取充值卡信息
     *
     * @param Request $request
     * @param CardModel $cardModel
     * @return void
     */
    public function get_card_info(
        Request $request,
        CardModel $cardModel
    ){
        $phone = $request->phone;
        if(empty($phone)) {
            return Message::show_message(false, '手机号码不能为空');
        }

        // 获取数据
        $cardInfo = $cardModel->where(['phone' => $phone])->first();
        if(empty($cardInfo)) {
            return Message::show_message(false, '暂未查询到手机号码充值记录');
        }
        // 集成card其他信息
        $cardInfo->topup_number = (int)env('TOPUP_NUMBER', '');
        $cardInfo->topup_amount = (int)env('TOPUP_AMOUNT', '');

        if ($cardInfo->use_time) { // 判断本月可用
            $lastUse = Carbon::parse($cardInfo->use_time);
            if ($lastUse->month === Carbon::now()->month) {
                $cardInfo->can_month = 0;
            }else{
                $cardInfo->can_month = 1;
            }
        }else{ // 没使用记录，肯定可以使用
            $cardInfo->can_month = 1;
        }
        
        return Message::show_message(true, 'success', $cardInfo);
    }
}
